#include "rclcpp/rclcpp.hpp" 
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals; 
class HelloWorldNode : public rclcpp::Node 
{ 
  public:
  HelloWorldNode()
  : Node("hello_world_node"), count_(0)
  {
    // 创建发布者，主题名为 /greeting，队列大小 10
    publisher_ = this->create_publisher<std_msgs::msg::String>("/greeting", 10);
    
    // 创建定时器，每秒触发一次回调函数
    timer_ = this->create_wall_timer( 1000ms, 
      std::bind(&HelloWorldNode::timer_callback, this));
    
    RCLCPP_INFO(this->get_logger(), "Hello World节点已启动");
  }
private: void timer_callback() {
    // 创建消息
    auto message = std_msgs::msg::String(); message.data = "Hello World " + 
    std::to_string(count_++);
    
    // 发布消息
    publisher_->publish(message);
    
    // 打印日志
    RCLCPP_INFO(this->get_logger(), "发布消息: '%s'", message.data.c_str());
  }
  
  rclcpp::TimerBase::SharedPtr timer_; 
  rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_; size_t count_;
};
int main(int argc, char * argv[]) {
  // 初始化ROS 2
  rclcpp::init(argc, argv);
  
  // 创建节点并保持运行
  auto node = std::make_shared<HelloWorldNode>(); rclcpp::spin(node);
  
  // 关闭ROS 2
  rclcpp::shutdown(); 
  return 0;
}
